//
//  LJMRankSubRoot_SonerViewController.m
//  wind
//
//  Created by 李金明 on 16/1/12.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMRankSubRoot_SonerViewController.h"
#import "LJMRankSubRoot_Soner_MainTableViewCell.h"
#import "LJMRankSubRoot_SonerTableViewCell.h"
#import "LJMRankSubRoot_MVModel.h"
#import <STKAudioPlayer.h>
#import "MVPlayerViewController.h" // mv界面

#pragma 点击播放_SYP
#import "SYPMusicPlay_ViewController.h"    // 音乐播放界面
#import "LJMListen_Model.h"

//关于button视图的位置
#define RANGE 5//距离上下5个
#define BUTTONWIDTH 80//button的宽度
#define BUTTONVIEWHEIGHT 40//VIEW的高度


@interface LJMRankSubRoot_SonerViewController () <UITableViewDataSource, UITableViewDelegate, collectionDelegate, changeToSon, changeToMV, changeTableView>
- (void)changeButton:(NSInteger)n;
- (void)changeCGFloat:(CGFloat)n;
- (void)changeSonData:(NSMutableArray *)data NSInter:(NSInteger)num;
- (void)changeMVModel:(LJMRankSubRoot_MVModel *)model;
- (void)tableViewCGFloat:(CGFloat)cgf;
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UIImageView *hyalineV;

@property (nonatomic, retain) UIImageView *titleImageV;
@property (nonatomic, retain) LJMRankSubRoot_Soner_MainTableViewCell *cell;
@property (nonatomic, retain) UIButton *sonBut;
@property (nonatomic, retain) UIButton *albumBut;
@property (nonatomic, retain) UIButton *MVBut;
@property (nonatomic, retain) UIButton *sonerBut;
@property (nonatomic, retain) UIImageView *headImageV;
@property (nonatomic, retain) UIView *headview;

@property (nonatomic, assign) NSInteger singerStates;  // 歌手的播放状态
@property (nonatomic, copy)  NSString *playingName;// 当期播放的第几个歌

@end

@implementation LJMRankSubRoot_SonerViewController

- (void)dealloc
{
    self.backDelegate = nil;
    [_playingName release];
    [_singer_id release];
    [_singer_name release];
    [_cell release];
    [_hyalineV release];
    [_tableV release];
    [_headview release];
    [_headImageV release];
    [_sonBut release];
    [_albumBut release];
    [_MVBut release];
    [_sonerBut release];
    [_titleImageV release];

    [super dealloc];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 处置化播放状态
    self.singerStates = 0;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建navigationbar
    [self creatNavigation];
    //创建个背景视图
    [self addtitleImage];

    //添加tableView
    [self addTableView];
    //添加个透明的图片当头视图
    [self addhyalineView];
//    添加个选择视图
        [self addchangerButView];
    //添加个小白条
        [self creatHeatView];
}


#pragma 添加个透明的图片当头视图
- (void)addhyalineView
{
    self.hyalineV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
    self.hyalineV.image = [UIImage imageNamed:@"navigationbar"];
    self.hyalineV.backgroundColor = [UIColor clearColor];
    self.hyalineV.userInteractionEnabled = YES;
    self.tableV.tableHeaderView = self.hyalineV;
    [_hyalineV release];
}
#pragma tableView的协议方法
#pragma 数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"maincell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == self.cell) {
        self.cell = [[LJMRankSubRoot_Soner_MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    self.cell.singer_id = self.singer_id;
    self.cell.collectdelegate = self;
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cell.changeDeldegate = self;
    self.cell.changeMVDeldegate = self;
    self.cell.changeTableView = self;
    return self.cell;
}
#pragma 跟着tabelView的随动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableV) {
        CGFloat flo = scrollView.contentOffset.y;
        if (flo < 0) {
            CGFloat fl = 25 + (flo / 5);
            self.titleImageV.frame = CGRectMake((flo / 5) / 2, -fl, self.view.frame.size.width - (flo / 5), self.view.frame.size.width - 2 * (flo / 5));
        }

       
    }
}
#pragma 添加tableView
- (void)addTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 40) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor clearColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.showsHorizontalScrollIndicator = NO;
    self.tableV.showsVerticalScrollIndicator = NO;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV.rowHeight = self.view.frame.size.height - BUTTONVIEWHEIGHT - 55;
    
    [self.view addSubview:self.tableV];
    [_tableV release];
    
}
#pragma /添加个移动的View在navigation上(小白条)
- (void)creatHeatView
{
    self.headImageV = [[UIImageView alloc] init];
    self.headImageV.frame = CGRectMake(self.sonBut.frame.origin.x, BUTTONVIEWHEIGHT - 3, BUTTONWIDTH, 3);
    self.headImageV.image = [UIImage imageNamed:@"head"];
    [self.headview addSubview:self.headImageV];
    [_headImageV release];
}
#pragma 添加个选择视图
- (void)addchangerButView
{
    self.headview = [[UIView alloc] init];
    self.headview.backgroundColor = [UIColor blackColor];
    self.headview.alpha = 0.5;
    [self.hyalineV addSubview:self.headview];
    [_headview release];
    [self.headview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(self.hyalineV.mas_bottom);
        make.height.mas_equalTo(@BUTTONVIEWHEIGHT);
    }];
    NSMutableArray *arr = @[].mutableCopy;
    self.sonBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sonBut setTitle:@"单曲" forState:UIControlStateNormal];
    [self.sonBut setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.sonBut setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.sonBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.albumBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.albumBut setTitle:@"专辑" forState:UIControlStateNormal];
    [self.albumBut setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.albumBut setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.albumBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.MVBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.MVBut setTitle:@"MV" forState:UIControlStateNormal];
    [self.MVBut setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.MVBut setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.MVBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.sonerBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sonerBut setTitle:@"歌手信息" forState:UIControlStateNormal];
    [self.sonerBut setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.sonerBut setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.sonerBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headview addSubview:self.sonBut];
    [self.headview addSubview:self.albumBut];
    [self.headview addSubview:self.MVBut];
    [self.headview addSubview:self.sonerBut];
    [arr addObject:self.sonBut];
    [arr addObject:self.albumBut];
    [arr addObject:self.MVBut];
    [arr addObject:self.sonerBut];
    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:BUTTONWIDTH leadSpacing:0 tailSpacing:0];
    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headview.mas_top).offset(RANGE);
        make.bottom.equalTo(self.headview.mas_bottom).offset(-RANGE);
    }];
    self.sonBut.selected = YES;
    self.albumBut.selected = NO;
    self.MVBut.selected = NO;
    self.sonerBut.selected = NO;
    
    
}
#pragma 选择界面上button的方法
- (void)buttonAction:(UIButton *)sender
{
    if (sender == self.sonBut) {
        self.sonBut.selected = YES;
        self.albumBut.selected = NO;
        self.MVBut.selected = NO;
        self.sonerBut.selected = NO;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        [self.cell.collectionV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    if (sender == self.albumBut) {
        self.sonBut.selected = NO;
        self.albumBut.selected = YES;
        self.MVBut.selected = NO;
        self.sonerBut.selected = NO;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
        [self.cell.collectionV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    if (sender == self.MVBut) {
        self.sonBut.selected = NO;
        self.albumBut.selected = NO;
        self.MVBut.selected = YES;
        self.sonerBut.selected = NO;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:2 inSection:0];
        [self.cell.collectionV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
    if (sender == self.sonerBut) {
        self.sonBut.selected = NO;
        self.albumBut.selected = NO;
        self.MVBut.selected = NO;
        self.sonerBut.selected = YES;
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:3 inSection:0];
        [self.cell.collectionV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }

}

#pragma 创建个背景视图
- (void)addtitleImage
{
    self.titleImageV = [[UIImageView alloc] init];
    [self.view addSubview:self.titleImageV];
    [self.titleImageV sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    [_titleImageV release];
    [self.titleImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(-20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(self.view.frame.size.width);
    }];
}
#pragma 创建navigationbar
- (void)creatNavigation
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
//给navigationbar一个透明图片然后让
    self.navigationController.navigationBar.translucent = YES;
    UIImage *image = [UIImage imageNamed:@"navigationbar"];
    
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    


}
#pragma 返回的动作
- (void)backAction
{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.alpha = 1;
    [self.navigationController popViewControllerAnimated:YES];
    [self.backDelegate SubBackNavigationDelegate];
    
}
#pragma LJMRankSubRoot_SonerTableViewCell的协议方法
#pragma 改变小白条
- (void)changeCGFloat:(CGFloat)n
{
//  self.headImageV.frame = CGRectMake(self.sonBut.frame.origin.x + n * BUTTONWIDTH, BUTTONVIEWHEIGHT - 3, BUTTONWIDTH, 3);
  self.headImageV.center = CGPointMake(self.sonBut.center.x + n * (self.albumBut.center.x - self.sonBut.center.x), self.titleImageV.bounds.origin.y + BUTTONVIEWHEIGHT - 1.5);
    
}
#pragma 改变button的状态
- (void)changeButton:(NSInteger)n
{
    if (0 == n) {
        self.sonBut.selected = YES;
        self.albumBut.selected = NO;
        self.MVBut.selected = NO;
        self.sonerBut.selected = NO;
 
    }
    if (1 == n) {
        self.sonBut.selected = NO;
        self.albumBut.selected = YES;
        self.MVBut.selected = NO;
        self.sonerBut.selected = NO;
    }
    if (2 == n) {
        self.sonBut.selected = NO;
        self.albumBut.selected = NO;
        self.MVBut.selected = YES;
        self.sonerBut.selected = NO;
    }
    if (3 == n) {
        self.sonBut.selected = NO;
        self.albumBut.selected = NO;
        self.MVBut.selected = NO;
        self.sonerBut.selected = YES;
    }
    
}

#pragma 点击跳转播放界面
#pragma Son的协议方法用于传递数值到音乐播放器
- (void)changeSonData:(NSMutableArray *)data NSInter:(NSInteger)num
{
    
    
    SYPMusicPlay_ViewController *SYPMusicPlayVC = [SYPMusicPlay_ViewController shareMusicPlayVC];
    SYPMusicPlayVC.MusicsArr = data;
    SYPMusicPlayVC.index = num;
    LJMListen_Model *mod = nil;
    if (0 != data) {
        mod = data[num];
        SYPMusicPlayVC.musicName.text = mod.name;
        SYPMusicPlayVC.singerName.text = mod.singerName;
        NSMutableDictionary *dic = mod.auditionListArr[1];
        SYPMusicPlayVC.musicURL = dic[@"url"];  // 点击的正在播放音乐的地址
    }

    if (SYPMusicPlayVC.player) {
        NSLog(@"已经有播放器");
        if (self.playingName == SYPMusicPlayVC.musicURL) {
            NSLog(@"点击的同一首音乐");
            
            switch (SYPMusicPlayVC.player.state) {
                case STKAudioPlayerStatePlaying:
                    NSLog(@"播放状态, 暂停");
                    [SYPMusicPlayVC.player pause];
                    SYPMusicPlayVC.states = 0;
                    break;
                    
                case STKAudioPlayerStatePaused:
                    NSLog(@"暂停状态, 播放");
                    [SYPMusicPlayVC.player resume];
                    SYPMusicPlayVC.states = 1;
                    break;
                    
                case STKAudioPlayerStateBuffering:
                    NSLog(@"缓冲状态");
                    break;
                    
                default:
                    break;
            }
        } else {
            NSLog(@"点击的不是同一首歌, 清空上一个播放器");
            [SYPMusicPlayVC.player stop];
            [SYPMusicPlayVC.player release];
            [SYPMusicPlayVC creatPlayerWithMusicName:SYPMusicPlayVC.musicURL];
            self.playingName = SYPMusicPlayVC.musicURL;
            SYPMusicPlayVC.states = 1;
            NSLog(@"已经创建新的播放器, 并开始播发");
            
        }
    } else {
        NSLog(@"还没有播放器");
    
        [SYPMusicPlayVC creatPlayerWithMusicName:SYPMusicPlayVC.musicURL];
        self.playingName = SYPMusicPlayVC.musicURL;
        NSLog(@"已经创建播放器");
        
    }
    
    // 发送改变状态的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeState" object:nil];
        
}



#pragma MV的协议方法用于传递数值到视频播放器
- (void)changeMVModel:(LJMRankSubRoot_MVModel *)model
{
//    NSLog(@"%@", model.myId);
    MVPlayerViewController *mvPlayerVC = [[MVPlayerViewController alloc] init];
    mvPlayerVC.mvStr = [[model.MVListArr firstObject] objectForKey:@"url"];
    mvPlayerVC.myID = model.myId;
    mvPlayerVC.videoName = model.videoName;
    mvPlayerVC.singerName = model.singerName;
    [self presentViewController:mvPlayerVC animated:YES completion:^{
        
        
    }];
}
#pragma 改变tableView的坐标
- (void)tableViewCGFloat:(CGFloat)cgf
{
    if (self.tableV.contentOffset.y > 0 && cgf < 0) {
     self.tableV.contentOffset = CGPointMake(0, self.tableV.contentOffset.y + cgf / 5);
    }
    if (self.tableV.contentOffset.y < 0 && cgf < 0) {
        self.tableV.contentOffset = CGPointMake(0, cgf);
    }
    if (self.tableV.contentOffset.y < self.view.bounds.size.height / 2 - 60 - BUTTONVIEWHEIGHT && cgf > 0) {
        self.tableV.contentOffset = CGPointMake(0, cgf);
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
